<?php
//模块名_控制名
class Product_ReceivingController extends Zend_Controller_Action
{
	public function preDispatch(){
		$this->tplDirectory = "product/receiving/";
	}
	
	public function indexAction(){
		$this->view->status = Product_Service_Receiving::$statusText;
		echo Cff::renderTpl('product/receiving/index.tpl',"layout");
	}

	public function loadlistAction(){
		
		$receivingCode = trim($this->_request->getParam("receivingCode",""));
		$status = trim($this->_request->getParam("status",""));
		$refId = trim($this->_request->getParam("refId",""));
		$customerId = trim($this->_request->getParam("customerId",""));
		
		$sortColumn = trim($this->_request->getParam("sortColumn",""));
		$sortMethod = trim($this->_request->getParam("sortMethod","asc"));
		$page = trim($this->_request->getParam("page",1));
		$pageSize = trim($this->_request->getParam("pageSize",20));
		$condition = array(
				"receiving_code" => $receivingCode,
				"status" => $status,
				"ref_id" => $refId,
				"customer_id" => $customerId,
				"sortColumn" => $sortColumn,
				"sortMethod" => $sortMethod,
		);		
		$orderby = array();
		$orderby[] = 'created desc';
		if($sortColumn){
			$sortStr = $sortColumn.' '.$sortMethod; 
			array_unshift($orderby,$sortStr);
		}
		
		$total = Product_Service_Receiving::getByCondition($condition,"count(*)");
		$list = Product_Service_Receiving::getByCondition($condition,"*", $page,$pageSize,$orderby);
		
		$serviceObj = new Product_Service_Product();
		foreach ($list as $key => $val){
			$products = array();
			$products = Product_Service_ReceivingItem::getAllByValue($val['receiving_code'],'receiving_code');
			foreach ($products as $k2 =>$v2){
				$productInfo = $serviceObj->getProductInfo($v2['product_id']);
				$products[$k2]['SKUNo'] = $productInfo['product_sku'];
				$products[$k2]['SKUName'] = $productInfo['product_title'];
				if($v2['remark']===NULL){
					$products[$k2]['remark'] = '';
				}
//				$totalQty += $v2['order_qty'];
			}
//			$list[$key]['order_type'] = $orderType[$val['order_type']];
			$list[$key]['products']=$products;
//			$list[$key]['totalQty']=$totalQty;
			
		}
		
		$data = array("ask" => 0,"total" => $total);
		if(!empty($list)){
			$data['ask'] = 1;
			$data['result'] = $list;
		}
		die(json_encode($data));
	}
	
	public function detailAction(){
		$receivingCode = trim($this->_request->getParam("code",""));
		$condition = array("receiving_code" => $receivingCode);
		$receiving = Product_Service_Receiving::getByCondition($condition);
		$items = array();
		if(is_array($receiving) && !empty($receiving)){
			$items = Product_Service_Receiving::getItems($condition);
		}
		$session = new Zend_Session_Namespace('userAuth');
		$this->view->userName=$session->name;
		$this->view->currentDate = date("Y年m月d日");
		$this->view->receiving = $receiving[0];
		$this->view->items = $items;
		echo $this->view->render('product/receiving/detail.tpl');
	}
	
	public function checkHasFirstAction(){
		$receivingCode = trim($this->_request->getParam("code",""));
		$condition = array("receiving_code" => $receivingCode);
		$receiving = Product_Service_Receiving::getByCondition($condition);
		$items = array();
		$hasFirst = false;//默认不是第一次收货
		if(is_array($receiving) && !empty($receiving)){
			$items = Product_Service_Receiving::getItems($condition);
		}
		foreach($items as $key => $value){
			$items[$key]['product']['package'] = Product_Service_Product::getByProductIdAndWarehouseId($value['product']['product_id'],$receiving[0]['warehouse_id']);
			if($value['product']['product_warehouse_status'] == 0){//第一次收货
				$hasFirst = true;
			}
		}
		die(json_encode($hasFirst));
	}
	
	public function receiveAction(){
		$receivingCode = trim($this->_request->getParam("code",""));
		$condition = array("receiving_code" => $receivingCode);
		$receiving = Product_Service_Receiving::getByCondition($condition);
		$items = array();
		$hasFirst = false;//默认不是第一次收货
		$firstItems = array();
		$submit=false;
		if(is_array($receiving) && !empty($receiving)){
			$items = Product_Service_Receiving::getItems($condition);
		}
		foreach($items as $key => $value){
			$items[$key]['product']['package'] = Product_Service_Product::getByProductIdAndWarehouseId($value['product']['product_id'],$receiving[0]['warehouse_id']);
			if($value['product']['product_warehouse_status'] == 0){//第一次收货
				$hasFirst = true;
				$firstItems[] = $items[$key];
			}
			$items[$key]['statustext']=Product_Service_ReceivingItem::$statusText[$value['status']];
			if($value['status']!=2){
				$submit=true;
			}
			if($value['status']==1){//分批收货
				$items[$key]['order_qty']=$items[$key]['order_qty']-$items[$key]['received_qty'];
				$items[$key]['received_qty']=0;
			}
			
		}

		$categorys = Product_Service_Product::getAllCategory();
		$this->view->categorys = $categorys;
		$packages = Product_Service_Package::getAllAvailable($receiving[0]['warehouse_id'],$receiving[0]['customer_id']);
		$this->view->packages = $packages;
		$this->view->receiving = $receiving[0];
		if($hasFirst){
			$this->view->items = $firstItems;
			echo $this->view->render('product/receiving/first.tpl');
			exit;
		}
		$this->view->submit = $submit;
		$this->view->items = $items;
		echo $this->view->render('product/receiving/receive.tpl');
	}
	
	public function itemInfoAction(){
		$itemId = trim($this->_request->itemId);
		$item = Product_Service_Receiving::getItemByItemId($itemId);
	
		$return = array("ask" => false);
		if($item){
			$return["ask"] = true;
			$return["result"] = $item;
		}
		die(json_encode($item));
	}
	
	public function receiveAddAction(){
		$itemId = trim($this->_request->itemId);
		$weight = trim($this->_request->weight);
		$receiveQty = trim($this->_request->receiveQty);
		$nonLabelQty = trim($this->_request->nonLabelQty);
		$isPacket = trim($this->_request->isPacket);
		$remark = trim($this->_request->remark);
		$labeled = 1;
		if($nonLabelQty != 0){
			$labeled = 0;
		}
		$row = array(
			"ri_id" => $itemId,
			"weight" => $weight,
			"received_qty" => $receiveQty,
			"non_labeled_qty" => $nonLabelQty,
			"labeled" => $labeled,
			"packaged" => $isPacket,
			"remark" => $remark,
		);
		$result = Product_Service_Receiving::receiveAdd($row);
		$return = array("ask" => false);
		if(isset($result['code'])){
			$return["message"] = $result['message'];
			die(json_encode($return));
		}
		if($result){
			$return["ask"] = true;
			$return["item"] = Product_Service_Receiving::getItemByItemId($itemId);
		}
		die(json_encode($return));
	}
	//提交收货
	public function receiveAddBatchAction(){
		$params = $this->_request->getParams();
		if(!$this->_request->isPost()){
			die(json_encode(array('ask'=>0,'error'=>'操作失败!')));
		}
		$rows=array();		
		foreach($params as $key => $param){
			$rows[$key] = $param;
		}
		
		$result=Product_Service_Receiving::procReceiveTransaction($rows);
		die(json_encode($result));
	}
	
	public function confirmProductAction(){
		$productId = $this->_request->productId;
		$result = array("ask" => Product_Service_Receiving::confirmProduct($productId));
		die(json_encode($result));
	}
	
	public function editProductAction(){
		$productId = trim($this->_request->productId);
		$category = trim($this->_request->category);
		$declared = trim($this->_request->declared);
		$packet = trim($this->_request->packet);
		$weight = trim($this->_request->weight);
		$warehouseId = trim($this->_request->warehouseId);
        $product_length = trim($this->_request->product_length);
        $product_width = trim($this->_request->product_width);
        $product_height = trim($this->_request->product_height);
		$row = array(
			"productId" => $productId,
			"category" => $category,
			"declared" => $declared,
			"packet" => $packet,
			"weight" => $weight,
			"warehouseId" => $warehouseId,
            "product_length" => $product_length,
            "product_width" => $product_width,
            "product_height" => $product_height,
		);
		$result = Product_Service_Receiving::editProduct($row);
		if(isset($result['code'])){
			$return["ask"] = false;
			$return["message"] = $result['message'];
			die(json_encode($return));
		}
		$return = array("ask" => Product_Service_Receiving::editProduct($row));
		die(json_encode($return)); 
	}
	
	public function barcodeAction(){
		Product_Service_Barcode::barcode($this->_request->code);
	}
}